//
//  CPDStockPriceStore.m
//  CorePlotDemo
//
//  NB: Price data obtained from Yahoo! Finance:
//  http://finance.yahoo.com/q/hp?s=AAPL
//  http://finance.yahoo.com/q/hp?s=GOOG
//  http://finance.yahoo.com/q/hp?s=MSFT
//
//  Created by Steve Baranski on 5/4/12.
//  Copyright (c) 2012 komorka technology, llc. All rights reserved.
//

#import "CPDStockPriceStore.h"
#import "DataBaseAccess.h"
#import "folyAppDelegate.h"

@interface CPDStockPriceStore ()


@end

@implementation CPDStockPriceStore

#pragma mark - Class methods

+ (CPDStockPriceStore *)sharedInstance
{
    static CPDStockPriceStore *sharedInstance;
    
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];      
    });
    
    return sharedInstance;
}

#pragma mark - API methods

- (NSArray *)tickerSymbols
{
    NSArray *symbols = nil;
    if (!symbols)
    {
        symbols = [[DataBaseAccess getAllIncomeType] mutableArrayValueForKey:kIncomeTypeName];
    }
    return symbols;
}

- (NSArray *)numberForIncomeSymbolsFromDate: (NSDate *)fromDate toDate: (NSDate *)toDate
{
    NSMutableArray *prices = nil;
    
//    NSDate *fromDate = [NSDate firstDayOfCurrentMonth];
//    NSDate *toDate = [NSDate firstDayOfNextMonth];
    NSLog(@"FROM %@ TO %@", fromDate, toDate);
    
    NSArray *listType = [DataBaseAccess getAllIncomeType];
    Wallet *w = ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).currentWallet;
    
    NSDecimalNumber *sum = [NSDecimalNumber zero];
    if(!prices)
    {
        prices = [[NSMutableArray alloc] init];
        for(IncomeType *i in listType)
        {
            NSDecimalNumber *numOfType = [DataBaseAccess numberSumIncomesOfType:i inWallet:w inTimeFrom:fromDate to:toDate];
            //NSDecimalNumber *numOfType = [DataBaseAccess numberSumIncomesOfType:i inWallet:w inMonth:10 andYear:2012];
            
            [prices addObject:numOfType];
            sum = [sum decimalNumberByAdding:numOfType];
        }
        NSLog(@"VALUE INCOME = %@", prices);
    }
    if([sum doubleValue] == 0.0)
    {
        prices = [[NSMutableArray alloc] init];
        for(IncomeType *i in listType)
        {
            [prices addObject:[NSDecimalNumber numberWithFloat:100.0]];
        }
        
        static UIAlertView *alert = nil;
        if(!alert)
        {
            alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"No income !" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
      
       ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).noData = [NSNumber numberWithBool:YES];
        
        NSLog(@"DATA = %@", ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).noData);
       
    }else {
        ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).noData = [NSNumber numberWithBool:NO];
    }
    
    
   
    return prices;
}

- (NSArray *)expenseSymbols
{
    NSArray *expenses = nil;
    if (!expenses)
    {
        expenses = [[DataBaseAccess getAllExpenseType] mutableArrayValueForKey:kExpenseTypeName];
    }
    return expenses;
}

- (NSArray *)numberForExpenseSymbolsFromDate: (NSDate *)fromDate toDate: (NSDate *)toDate
{
    NSMutableArray *prices = nil;
    
//    NSDate *fromDate = [NSDate firstDayOfCurrentMonth];
//    NSDate *toDate = [NSDate firstDayOfNextMonth];
    NSLog(@"FROM %@ TO %@", fromDate, toDate);
    
    NSArray *listType = [DataBaseAccess getAllExpenseType];
    Wallet *w = ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).currentWallet;
    
    NSDecimalNumber *sum = [NSDecimalNumber zero];
    
    if(!prices)
    {
        prices = [[NSMutableArray alloc] init];
        for(ExpenseType *i in listType)
        {
            NSDecimalNumber *numOfType = [DataBaseAccess numberSumExpensesOfType:i inWallet:w inTimeFrom:fromDate to:toDate];
            
            [prices addObject:numOfType];
            sum = [sum decimalNumberByAdding:numOfType];
        }
        NSLog(@"VALUE = %@", prices);
    }
    
    if([sum doubleValue] == 0.0)
    {
        prices = [[NSMutableArray alloc] init];
        for(ExpenseType *i in listType)
        {
            [prices addObject:[NSDecimalNumber numberWithFloat:100.0]];
        }
        
        static UIAlertView *alert = nil;
        if(!alert)
        {
            alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"No expense !" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
      
        ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).noData = [NSNumber numberWithBool:YES];
    }else {
        ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).noData = [NSNumber numberWithBool:NO];
    }
    
    
    
    return prices;
}

- (NSArray *)datesInWeek
{
    static NSArray *dates = nil;
    if (!dates)
    {
        dates = [NSArray arrayWithObjects:
                 @"1", 
                 @"2", 
                 @"3",
                 @"4", 
                 @"5",
                 @"6",
                 @"7",
                 @"8",
                 @"9",
                 @"10",
                 @"11",
                 @"12",
                 nil];
    }
    return dates;
}

- (NSArray *)weeklyPrices:(NSString *)tickerSymbol
{
    if ([CPDTickerSymbolAAPL isEqualToString:[tickerSymbol uppercaseString]] == YES)
    {
        return [self weeklyAaplPrices];
    }
    else if ([CPDTickerSymbolGOOG isEqualToString:[tickerSymbol uppercaseString]] == YES)
    {
        return [self weeklyGoogPrices];
    }
    else if ([CPDTickerSymbolMSFT isEqualToString:[tickerSymbol uppercaseString]] == YES)
    {
        return [self weeklyMsftPrices];
    }
    return [NSArray array];
}

- (NSArray *)datesInMonth
{
    static NSArray *dates = nil;
    if (!dates)
    {
        dates = [NSArray arrayWithObjects:
                 @"1", 
                 @"2", 
                 @"3",
                 @"4", 
                 @"5",
                 @"6",
                 @"7",
                 @"8",
                 @"9",
                 @"10",
                 @"11",
                 @"12",                
                 nil];
    }
    return dates;
}

- (NSArray *)monthlyPrices:(NSString *)tickerSymbol
{
    if ([CPDTickerSymbolAAPL isEqualToString:[tickerSymbol uppercaseString]] == YES)
    {
        return [self monthlyAaplPrices];
    }
    else if ([CPDTickerSymbolGOOG isEqualToString:[tickerSymbol uppercaseString]] == YES)
    {
        return [self monthlyGoogPrices];
    }
    else if ([CPDTickerSymbolMSFT isEqualToString:[tickerSymbol uppercaseString]] == YES)
    {
        return [self monthlyMsftPrices];
    }
    return [NSArray array];
}

#pragma mark - Private behavior

- (NSArray *)weeklyAaplPrices
{
    NSArray *prices = nil;
    Wallet *w = ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).currentWallet;
    int currentYear = ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).currentYear;
    if (!prices)
    {
        prices = [NSArray arrayWithObjects:
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:1 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:2 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:3 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:4 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:5 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:6 andYear:currentYear],  
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:7 andYear:currentYear],
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:8 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:9 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:10 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:11 andYear:currentYear], 
                  [DataBaseAccess numberSumIncomesInWallet:w inMonth:12 andYear:currentYear], 
                  nil];
    }
    return prices;
}

- (NSArray *)weeklyGoogPrices
{
    NSArray *prices = nil;
    Wallet *w = ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).currentWallet;
    int currentYear = ((folyAppDelegate *)[[UIApplication sharedApplication]delegate]).currentYear;
    if (!prices)
    {
        prices = [NSArray arrayWithObjects:
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:1 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:2 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:3 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:4 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:5 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:6 andYear:currentYear],  
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:7 andYear:currentYear],
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:8 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:9 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:10 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:11 andYear:currentYear], 
                  [DataBaseAccess numberSumExpensesInWallet:w inMonth:12 andYear:currentYear], 
                  nil];
    }
    return prices;
}

- (NSArray *)weeklyMsftPrices
{
    static NSArray *prices = nil;
    if (!prices)
    {
        prices = [NSArray arrayWithObjects:
                  [NSDecimalNumber numberWithFloat:32.12], 
                  [NSDecimalNumber numberWithFloat:31.92], 
                  [NSDecimalNumber numberWithFloat:32.20], 
                  [NSDecimalNumber numberWithFloat:32.11], 
                  [NSDecimalNumber numberWithFloat:31.98],  
                  [NSDecimalNumber numberWithFloat:603.00],  
                  [NSDecimalNumber numberWithFloat:603.00],  
                  nil];
    }
    return prices;
}

- (NSArray *)monthlyAaplPrices
{
    return [self weeklyAaplPrices];
}

- (NSArray *)monthlyGoogPrices
{
    return [self weeklyGoogPrices];
}

- (NSArray *)monthlyMsftPrices
{
    NSMutableArray *prices = nil;
    if(!prices)
    {
        prices = [[NSMutableArray alloc] init];
        NSArray *incomes = [self monthlyAaplPrices];
        NSArray *expenses = [self monthlyGoogPrices];
        for(int i = 0; i < [[self datesInMonth] count]; i++)
        {
            NSDecimalNumber *decNum = [incomes objectAtIndex:i];
            decNum = [decNum decimalNumberBySubtracting:[expenses objectAtIndex:i]];
            [prices addObject:decNum];
        }
    }
    
    return prices;
}



@end
